import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../component/back.dart';
import '../component/btn.dart';
import '../component/login_input.dart';
import '../theme/DQColor.dart';

class LoginPageView extends StatefulWidget {
  const LoginPageView({super.key});

  @override
  State<LoginPageView> createState() => _LoginPageViewState();
}

class _LoginPageViewState extends State<LoginPageView> {
  // 定义一个 GlobalKey，用于标识 Form
  final _formKey = GlobalKey<FormState>();

  // 定义控制器，用于获取输入框的值
  final TextEditingController _nameController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();
  final TextEditingController _codeController = TextEditingController();

  void _submitForm() {
    if (_formKey.currentState!.validate()) {
      // 验证通过，执行登录逻辑
      print('登录成功');
    } else {
      // 验证失败，显示错误提示
      print('登录失败');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SizedBox(
        width: MediaQuery.of(context).size.width,
        child: Stack(
          children: [
            ImageFiltered(
              imageFilter: ImageFilter.blur(sigmaX: 8.0, sigmaY: 8.0),
              child: Image.asset(
                'assets/images/splash.jpg',
                width: MediaQuery.of(context).size.width,
                height: MediaQuery.of(context).size.height,
                fit: BoxFit.cover,
              ),
            ),
            Center(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Form(
                  // 设置 GlobalKey
                  key: _formKey,
                  child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        // 姓名输入框
                        SizedBox(
                          width: 260,
                          child: CustomTextField(
                            controller: _nameController,
                            hintText: '用户名',
                            obscureText: false,
                            validator: (value) {
                              if (value == null || value.isEmpty) {
                                return '请输入姓名';
                              }
                              return null;
                            },
                          ),
                        ),
                        SizedBox(height: 16.0),
                        SizedBox(
                          width: 260,
                          child: CustomTextField(
                            controller: _passwordController,
                            hintText: '密码',
                            obscureText: true,
                            validator: (value) {
                              if (value == null || value.isEmpty) {
                                return '请输入密码';
                              }
                              return null;
                            },
                          ),
                        ),
                        SizedBox(height: 16.0),
                        SizedBox(
                          width: 260,
                          child: CustomTextField(
                            controller: _codeController,
                            hintText: '口令',
                            obscureText: false,
                            validator: (value) {
                              if (value == null || value.isEmpty) {
                                return '请输入口令';
                              }
                              return null;
                            },
                          ),
                        ),
                        SizedBox(height: 32.0),
                        // 提交按钮
                        GradientButton(
                          onPressed: _submitForm,
                          text: '提交表单',
                        )
                      ]),
                ),
              ),
            ),
            Back(),
          ],
        ),
      ),
    );
  }
}
